package com.huawei.hgip.paintgame.util;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import com.huawei.hgip.paintgame.m.data.UserProfile;

public class PGUtil {

    public static final int BC_PIC_SEARCH = 11100;
    public static final int BC_PIC_SEARCH_VOTE = 11200;
    
    
    //public static final int DRAW_SEARCH = 1102;
    //public static final int PIC_SEARCH_OK = 1001;
    //public static final int DRAW_SEARCH_OK = 1002;

    public static final int BC_SYS_SUCCESS = 20100;
    public static final int BC_SYS_ERR = 20200;

    //public static final int NO_SUIT_RESULT = 2002;
    //public static final int DB_ERROR = 2003;

    public static final byte VOTE_GOOD = 1;
    public static final byte VOTE_BAD = 2;

    public static final int BC_USER_REGISTER = 10100;
    public static final int BC_USER_LOGIN = 10200;
    public static final int BC_USER_LOGOUT = 10300;
    
	//public static final int PIC_SEARCH = 1101;
	//public static final int DRAW_SEARCH = 1102;
	//public static final int PIC_SEARCH_OK = 1001;
	//public static final int DRAW_SEARCH_OK = 1002;
	//public static final int SERVER_ERR = 2001;
	//public static final int NO_SUIT_RESULT = 2002;
	//public static final int VOTE_GOOD = 1201;
	public static final int VOTE_MIDDLE = 1202;
	//public static final int VOTE_BAD = 1203;
	//public static final String MHOST = "10.137.43.33";
	public static final String MHOST = "10.138.51.41";
	//public static final String MHOST = "58.60.106.31";
	public static final int MPORT = 9999;	
	public static final String ZZYY_HOME = "/sdcard/zzyy/";

	public static final String EXCE_NET_OR_SERVER = "无法连接系统服务，请您稍候再试";
	public static final String EXCE_SDCARD = "读写SD卡失败";
	public static final String EXCE_UNKNOW = "系统发生未知异常";
	public static final String EXCE_APN_WAP = "请检查您的移动网络设置，如接入点名称为WAP，则可能无法使用本应用";
	
//	private String HOST = "10.70.110.130";
//	private int PORT = 9999;
	// 网上抄来的，将字节转成 int。b 长度不得小于 4，且只会取前 4 位。
	public static int b2i(byte[] b) {
		int value = 0;
		for (int i = 0; i < 4; i++) {
			int shift = (4 - 1 - i) * 8;
			value += (b[i] & 0x000000FF) << shift;
		}
		return value;
	}
	
	// 网上抄来的，将 int 转成字节
	public static byte[] i2b(int i) {
		return new byte[] { (byte) ((i >> 24) & 0xFF),
				(byte) ((i >> 16) & 0xFF), (byte) ((i >> 8) & 0xFF),
				(byte) (i & 0xFF) };
	}
	
	/**
	 * 根据业务类型获得socket，创建与服务器通信的socket，并负责校验码，业务类型和用户id等公共信息的写入，
	 * @param busiType 业务类型
	 * @return 返回socket对象，供获取inputStream，outputStream继续进行具体业务的读写操作
	 * @throws SocketException 
	 * @throws UnknownHostException
	 * @throws IOException
	 */
	public static Socket getSocket(int busiType) throws SocketException {
				
		//Socket sct = new Socket(MHOST, MPORT);
		Socket sct;
		try{
		sct = new Socket();
		sct.connect(new InetSocketAddress(MHOST, MPORT), 5500);
		sct.setSoTimeout(25000);
		DataOutputStream ops = new DataOutputStream(
				sct.getOutputStream());		
		
		if (busiType == BC_USER_REGISTER){
			//写特殊公钥
			ops.write(i2b(BC_USER_REGISTER));
			//写业务类型
			ops.write(i2b(busiType));
			return sct;
		}
		
		int userid = getUserID();
		//TODO 获得用户ID
		
		//公钥
		int snumber = new Integer(userid).toString().hashCode();

		
		//写公钥
		ops.write(i2b(snumber));
		//写业务类型
		ops.write(i2b(busiType));
		//写用户id
		ops.write(i2b(userid));
		
		}catch(IOException iex){
			throw new SocketException();
		}
		
		return sct;
	}
	
	public static int getUserID(){
		//获得用户ID	
		return UserProfile.userId;
	}
	
	/**
	 * 向output stream中写入一个字符串,格式为int 字符串长度+ String 字符串内容
	 * @param writeText 要写入的内容
	 * @param os 要写入的output stream
	 * @throws IOException 
	 */
	public static void writeStringWithLength(String writeText, OutputStream os) throws IOException{
		byte[] str_bytes = writeText.getBytes();
		os.write(PGUtil.i2b(str_bytes.length)); // 输出字符串长度
		os.write(str_bytes); // 输出字符串
	}
	
	/**
	 * 从intput stream中读取一个字符串,intput stream中的格式为int 字符串长度+ String 字符串内容
	 * @param is 要写入的input stream
	 * @return 读出的String
	 * @throws IOException
	 */
	public static String readStringWithLength(InputStream is) throws IOException{
		int str_len = readInteger(is);
		byte[] result = new byte[str_len];
		is.read(result);
		return new String(result);
	}
	
    // 读取一个数字
    public static int readInteger(InputStream is) throws IOException {
        byte[] bytes = new byte[4];
        is.read(bytes);
        return b2i(bytes);
    }
}